﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWebPagesStarterKit.Controls;
using MyWebPagesStarterKit;
using System.Data;
using System.IO;

public partial class SectionControls_ImageGallery : SectionControlBaseClass
{

    private ImageGallery _section;
    protected string SectionID;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        SectionID = _section.SectionId;

        if (ViewMode == ViewMode.Edit)
        {
            multiview.SetActiveView(editView);
            if (ViewState["Edit"] != null)
            {
                DataRow row = _section.GetRow(Convert.ToString(ViewState["Edit"]));
                ImageGallery.ImageEntry entry = (ImageGallery.ImageEntry)row["ImageEntry"];
                if (entry != null)
                {
                    txtTitle.Text = entry.InternalTitle;
                    txtDescription.Text = entry.Description;
                }
            }

            gvImages.DataSource = _section.Entries;
            gvImages.DataBind();
        }
        else
        {
            multiview.SetActiveView(readonlyView);
            multiview.SetActiveView(readonlyView);
            litTitle.Text = _section.Title;
            List<ImageGallery.ImageEntry> list = _section.GetImageEntries();
            if (list.Count > 0)
            {
                foreach (ImageGallery.ImageEntry image in list)
                    litImages.Text += string.Format("<li><a href=\"#\" rel=\"/ImageHandler.ashx?width=500&height=450&pg={0}&image=~/App_Data/ImageGallery/{4}/Image/{1}\" title=\"{3}\" class=\"toolTip\"><img src=\"/ImageHandler.ashx?pg={0}&UploadedFile=true&image=~/App_Data/ImageGallery/{4}/Image/thumbs/{2}\" alt=\"{3}\" class=\"gallery\" /></a></li>\n", "{0}", image.ImageAlternateImagePath, image.ImagePath, image.InternalTitle, _section.SectionId);
            }
        }

    }

    protected void lstGalleryEdit_ItemCommand(object source, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "edit_entry":
                ViewState["Edit"] = gvImages.DataKeys[int.Parse(e.CommandArgument.ToString())].Value;
                break;
            case "delete_entry":
                DataRow row = _section.GetRow(gvImages.DataKeys[int.Parse(e.CommandArgument.ToString())].Value.ToString());
                try
                {
                    ImageGallery.ImageEntry entry = (ImageGallery.ImageEntry)row["ImageEntry"];
                    if (entry != null)
                    {
                        string file = Server.MapPath("~/App_Data/ImageGallery/Images/") + entry.ImagePath;
                        if (File.Exists(file))
                            File.Delete(file);

                        string altFile = Server.MapPath("~/App_Data/ImageGallery/Images/") + entry.ImageAlternateImagePath;
                        if (File.Exists(altFile))
                            File.Delete(altFile);
                    }
                }
                catch { }
                _section.Entries.Rows.Remove(row);
                _section.Entries.AcceptChanges();
                _section.SaveData();
                break;
        }
    }

    protected void Save(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtGalleryTitle.Text))
            return;
        if (string.IsNullOrEmpty(txtTitle.Text))
            return;

        DataRow row = null;
        ImageGallery.ImageEntry entry = null;
        if (ViewState["Edit"] == null)
        {
            row = _section.Entries.NewRow();
            row["Guid"] = Guid.NewGuid().ToString();
            _section.Entries.Rows.InsertAt(row, 0);
            entry = new ImageGallery.ImageEntry();
        }
        else
        {
            row = _section.GetRow(Convert.ToString(ViewState["Edit"]));
            if (row["ImageEntry"] != null)
                entry = (ImageGallery.ImageEntry)row["ImageEntry"];
            else
                entry = new ImageGallery.ImageEntry();
        }

        _section.Title = txtGalleryTitle.Text;
        row["Title"] = txtTitle.Text;

        //Must pass to create image entry
        if (txtTitle.Text != string.Empty)
        {
            entry.InternalTitle = txtTitle.Text;
            entry.Description = txtDescription.Text;

            row["ImageEntry"] = entry;
        }

        row.AcceptChanges();
        _section.SaveData();
    }

    protected void AddNew(object sender, EventArgs e)
    {
        ViewState["Edit"] = null;
        txtTitle.Text = string.Empty;
        txtDescription.Text = string.Empty;
    }

    public override bool HasAdminView
    {
        get { return true; }
    }

    public override string InfoUrl
    {
        get { return ""; }
    }

    public override ISection Section
    {
        set
        {
            if (value is ImageGallery)
                _section = (ImageGallery)value;
            else
                throw new ArgumentException("Section must be of type ImageGallery");
        }
        get { return _section; }
    }
}
